package com.example.springbootmybatis.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpConnectUtil {  
    private static Logger logger = LoggerFactory.getLogger(HttpConnectUtil.class);
    
    /**
     * 远程post发送数据
     * @param urlStr 远程地址
     * @return 返回内容
     */
    public String sendHttpGet(String urlStr) {
        return this.sendHttpGet(urlStr, null);
    }
    
    /**
     * 远程get发送数据
     * @param urlStr 远程地址
     * @param map 文件头
     * @return 返回内容
     */
    public String sendHttpGet(String urlStr, Map<String, String> map) {
        URL url = null;
        HttpURLConnection urlconn = null;
        try {
            url = new URL(urlStr);
            urlconn = (HttpURLConnection) url.openConnection();
            urlconn.setRequestProperty("content-type", "application/html");
            urlconn.setRequestProperty("contentType", "UTF-8");
            
            if(map != null){
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    urlconn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }

            urlconn.setRequestMethod("GET");
            urlconn.setDoInput(true);
            urlconn.setDoOutput(true);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } 

        String reString = "";
        BufferedReader rd = null;
        try {
            rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
            StringBuffer sbBack = new StringBuffer();
            String val;
            while ((val = rd.readLine()) != null) {
                sbBack.append(val);
            }
            reString = sbBack.toString();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } finally {
            try {
                if (rd != null)
                    rd.close();
            } catch (IOException e) {
                rd = null;
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            }
            urlconn.disconnect();
        }
        return reString;
    }
    
    /**
     * 远程post发送数据
     * @param urlStr 远程地址
     * @param content 内容
     * @return 返回内容
     */
    public static String sendHttpPost(String urlStr, String content) {
        return sendHttpPost(urlStr, content, null);
    }
    
    /**
     * 远程post发送数据
     * @param urlStr 远程地址
     * @param content 内容
     * @param map 文件头
     * @return 返回内容
     */
    public static String sendHttpPost(String urlStr, String content, Map<String, String> map) {
        URL url = null;
        HttpURLConnection urlconn = null;
        OutputStreamWriter out = null;
        try {
            url = new URL(urlStr);
            urlconn = (HttpURLConnection) url.openConnection();
            urlconn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            urlconn.setRequestProperty("contentType", "UTF-8");
            
            if(map != null){
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    urlconn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }

            urlconn.setRequestMethod("POST");
            urlconn.setDoInput(true);
            urlconn.setDoOutput(true);

            out = new OutputStreamWriter(urlconn.getOutputStream(), "UTF-8");
            out.write(content);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    out = null;
                    logger.error(e.getMessage(), e);
                }
            }
        }

        String reString = "";
        BufferedReader rd = null;
        try {
            rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
            StringBuffer sbBack = new StringBuffer();
            String val;
            while ((val = rd.readLine()) != null) {
                sbBack.append(val);
            }
            reString = sbBack.toString();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } finally {
            try {
                if (rd != null)
                    rd.close();
            } catch (IOException e) {
                rd = null;
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            }
            urlconn.disconnect();
        }
        return reString;
    }
    
  /*  public static void main(String args[]){
    	HttpConnectUtil hu = new HttpConnectUtil();
    	String re = hu.sendHttpGet("http://10.0.0.30:8084/car/match/score?brandId=18&seriesId=192&modelId=1635&brandGroupId=0&pareSys=0",null);
    	System.out.println("re=="+re);
    }*/
}
